package com.msmall.erp.sso.manager.impl;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.msmall.base.MD5;
import com.msmall.base.rms.manager.AdministerManager;
import com.msmall.base.rms.po.Administer;
import com.msmall.erp.base.ReturnLogin;
import com.msmall.erp.sso.manager.SSOManager;

@Service("ssoManager")
public class SSOManagerImpl implements SSOManager {

	@Autowired
	private AdministerManager administerManager;
	
	public JSONObject validate(String username, String plainPwd) throws Exception{
		JSONObject json = new JSONObject();
		Administer administer = administerManager.getAdminister(username);
		if(administer == null){
			json.put(ReturnLogin.RETURN_CODE_KEY, ReturnLogin.RETURN_CODE_FAILED_USERNAME);
		}else{
			String enctryPwd = MD5.MD5Code(plainPwd);
			if(enctryPwd.equals(administer.getPassword())){
				json.put(ReturnLogin.RETURN_CODE_KEY, ReturnLogin.RETURN_CODE_SUCCESS);
				json.put(ReturnLogin.RETURN_KEY_ADMINISTER, administer);
			}else{
				json.put(ReturnLogin.RETURN_CODE_KEY, ReturnLogin.RETURN_CODE_FAILED_PASSWORD);
			}
		}
		return json;
	}
}
